#include "CodeHandle.h"

CodeHandle::CodeHandle()
{


   // ${EventName}		  	//Event 的名字，egs:getDom

   // ${MethodRelativePath} 	//转换方法相对路径，egs: ./SMSTin/SMSTin.exe

   // ${SrcFileRelativePath} 	//输入文件源文件的相对路径, egs：./ch_grd.dat

    ResEventTempStr.append("            if (resEventName.equals(\"${EventName}\")) {").append("\n");
    ResEventTempStr.append("					String commandStr = PathUtil.relativePathToAbsolutePath(saveTmpDir,\"${MethodRelativePath}\")+\" \";").append("\n");
    ResEventTempStr.append("					commandStr += UdxFilePath+\" \";").append("\n");
    ResEventTempStr.append("					commandStr += PathUtil.relativePathToAbsolutePath(saveTmpDir,\"${SrcFileRelativePath}\")+\" \";").append("\n");
    ResEventTempStr.append("					commandStr +=\"UdxToSrc\";").append("\n");
    ResEventTempStr.append("			        ProcessUtil.callCmd(commandStr);").append("\n");
    ResEventTempStr.append("                    forDelPaths.add(PathUtil.relativePathToAbsolutePath(saveTmpDir,\"${SrcFileRelativePath}\"));").append("\n");
    ResEventTempStr.append("                    continue;").append("\n");
    ResEventTempStr.append("			}").append("\n");



    // ${EventName}			  //结果文件事件名，egs:Obc_Xy

    //${MethodRelativePath} 	//转换方法相对路径，egs: ./Obc_Xy/Obc_Xy.exe

    // ${SrcFileRelativePath} 	//输出文件源文件的相对路径, egs：./ch_obc_xy.dat

    NoResEventTempStr.append("          if (NoresEventName.equals(\"${EventName}\")) {").append("\n");
    NoResEventTempStr.append("                  UdxDataset NoResDataset = new UdxDataset(dllspath);").append("\n");
    NoResEventTempStr.append("                  String commandStr = PathUtil.relativePathToAbsolutePath(saveTmpDir,\"${MethodRelativePath}\")+\" \";").append("\n");
    NoResEventTempStr.append("                  commandStr += PathUtil.relativePathToAbsolutePath(saveTmpDir,\"${SrcFileRelativePath}\")+\" \";").append("\n");
    NoResEventTempStr.append("                  commandStr += RsltUdxTempPath+\" \";").append("\n");
    NoResEventTempStr.append("                  commandStr +=\"SrcToUdx\";").append("\n");
    NoResEventTempStr.append("                  if(!ProcessUtil.callCmd(commandStr)){").append("\n");
    NoResEventTempStr.append("                      System.out.println(\"call cmd error.\");").append("\n");
    NoResEventTempStr.append("                      return ;").append("\n");
    NoResEventTempStr.append("                   }").append("\n");
    NoResEventTempStr.append("                  NoResDataset.LoadFromXmlFile(RsltUdxTempPath);").append("\n");
    NoResEventTempStr.append("                  resultUdx.put(i,NoResDataset);").append("\n");
    NoResEventTempStr.append("                  reStrLength = resultUdx.get(i).FormatToXmlStream().length();").append("\n");
    NoResEventTempStr.append("                  ResponseStr += stateId + \"[\\t\\t]\" + NoresEventName + \"[\\t\\t]\"").append("\n");
    NoResEventTempStr.append("                  + \"UNKNOWN\" + \"[\\t\\t]\" + reStrLength + \"[\\t\\t\\t]\";").append("\n");
    NoResEventTempStr.append("                  forDelPaths.add(RsltUdxTempPath);").append("\n");
    NoResEventTempStr.append("                  forDelPaths.add(PathUtil.relativePathToAbsolutePath(saveTmpDir,\"${SrcFileRelativePath}\"));").append("\n");
    NoResEventTempStr.append("                  continue;").append("\n");
    NoResEventTempStr.append("			}").append("\n");


}

CodeHandle::~CodeHandle(){
    this->ResEventTempStr="";
    this->NoResEventTempStr="";
    this->MainStr = "";
    this->InputFConversionMethodsStr = "";
    this->ResultConversionMethodsStr = "";
}

//init:get template main file content
bool CodeHandle::init(const QString MainFilePath){
    if(MainFilePath.isEmpty()){
        qWarning()<<"Main template file path can not be null."<<endl;
        return false;
    }
    QFile MainFile(MainFilePath);
    if(!MainFile.exists()){
        qWarning()<<"Main template file does not exist."<<endl;
        return false;
    }
    if(!MainFile.open(QIODevice::ReadWrite | QIODevice::Text)){
        qWarning()<<"Failed to open template Main.java."<<endl;
        return false;
    }
    QString MainAllStr(MainFile.readAll());
    MainFile.close();

    if(!MainAllStr.isEmpty()){
        this->MainStr = MainAllStr;
    }else{
        qWarning()<<"the content of template Main.java is null."<<endl;
        return false;
    }
    return true;

}

bool CodeHandle::setMdlName(const QString MdlName){
    if(MdlName.isEmpty()){
        qWarning()<<"Mdl name can not be null."<<endl;
        return false;
    }
    this->MainStr.replace("${MdlFileName}",MdlName);
    return true;
}

bool CodeHandle::setModelRelativeLocation(const QString ModelPath){
    if(ModelPath.isEmpty()){
        qWarning()<<"Model path can not be null."<<endl;
        return false;
    }
    this->MainStr.replace("${ModelRelativeLocation}",ModelPath);
    return true;
}

bool CodeHandle::setInputParameter(const QString InputParameter){
    if(InputParameter.isEmpty()){
         this->MainStr.replace("${InputParameter}","");
       return true;
    }else{
        this->MainStr.replace("${InputParameter}",InputParameter);
        return true;
    }
}

bool CodeHandle::beginAddResEvent(){
    this->InputFConversionMethodsStr="\n";
    return true;
}

bool CodeHandle::addResEvent(const QString EventName, const QString MethodRelativePath, const QString SrcFileRelativePath){
    QString OneResStr = this->ResEventTempStr;

    if(EventName.isEmpty()){
        qWarning()<<"Event name can not be null."<<endl;
        return false;
    }
    if(MethodRelativePath.isEmpty()){
        qWarning()<<"Method relative path can not be null."<<endl;
        return false;
    }
    if(SrcFileRelativePath.isEmpty()){
        qWarning()<<"SrcFile  relative  path can not be null."<<endl;
        return false;
    }
    OneResStr.replace("${EventName}",EventName);
    OneResStr.replace("${MethodRelativePath}",MethodRelativePath);
    OneResStr.replace("${SrcFileRelativePath}",SrcFileRelativePath);
    this->InputFConversionMethodsStr +=OneResStr;
    return true;
}

bool CodeHandle::endAddResEvent(){
    if(this->InputFConversionMethodsStr.isEmpty()){
        qWarning()<<"have no content to add."<<endl;
        return false;
    }
    if(!this->setInputFConversionMethods(this->InputFConversionMethodsStr)){
        qWarning()<<"Set input conversion  methods string error."<<endl;
        return false;
    }
    return true;
}

bool CodeHandle::beginAddNoResEvent(){
    this->ResultConversionMethodsStr="\n";
    return true;
}

bool CodeHandle::addNoResEvent(const QString EventName, const QString MethodRelativePath, const QString SrcFileRelativePath){
    QString OneResStr = this->NoResEventTempStr;

    if(EventName.isEmpty()){
        qWarning()<<"Event name can not be null."<<endl;
        return false;
    }
    if(MethodRelativePath.isEmpty()){
        qWarning()<<"Method relative path can not be null."<<endl;
        return false;
    }
    if(SrcFileRelativePath.isEmpty()){
        qWarning()<<"SrcFile  relative  path can not be null."<<endl;
        return false;
    }
    OneResStr.replace("${EventName}",EventName);
    OneResStr.replace("${MethodRelativePath}",MethodRelativePath);
    OneResStr.replace("${SrcFileRelativePath}",SrcFileRelativePath);
    this->ResultConversionMethodsStr +=OneResStr;
    return true;
}

bool CodeHandle::endAddNoResEvent(){
    if(this->ResultConversionMethodsStr.isEmpty()){
        qWarning()<<"have no content to add."<<endl;
        return false;
    }
    if(!this->setResultConversionMethods(this->ResultConversionMethodsStr)){
        qWarning()<<"Set result conversion  methods string error."<<endl;
        return false;
    }
    return true;
}

bool CodeHandle::setInputFConversionMethods(const QString InputFConversionMethodsStr){
    if(InputFConversionMethodsStr.isEmpty()){
        qWarning()<<"Input conversion  methods string can not be null."<<endl;
        return false;
    }else{
        this->MainStr.replace("${InputConversionMethods}",InputFConversionMethodsStr);
        return true;
    }

}

bool CodeHandle::setResultConversionMethods(const QString ResultConversionMethodsStr){
    if(ResultConversionMethodsStr.isEmpty()){
            qWarning()<<"Input conversion  methods string can not be null."<<endl;
            return false;
        }else{
            this->MainStr.replace("${ResultConversionMethods}",ResultConversionMethodsStr);
            return true;
        }
}

QString CodeHandle::returnMainStr(){
    return this->MainStr;
}

bool CodeHandle::SaveMainFile(QString FilePath, QString ContentStr){
    if(!SaveFile(FilePath,ContentStr)){
        qWarning()<<"Save main file error."<<endl;
        return false;
    }
    return true;
}

bool CodeHandle::SaveMainFile(QString FilePath){
    if(!SaveFile(FilePath,this->MainStr)){
        qWarning()<<"Save main file error."<<endl;
        return false;
    }
    return true;
}












